<?php

use Biz\model\package\OrderLogisticsLog;
use Biz\model\package\PackageLogistics;
use Biz\service\package\PackageOrder;
use Doraemon\model\Phone;
use hhz\constant\basic\UserExtendPhoneScene;

class OrderLogisticsAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;
    public function main()
    {
        $orderNo = $this->getRequest()->getPost('order_no');

        $orderInfo = PackageOrder::getOrderInfo($orderNo);
        if (!empty($orderInfo['consignee_phone_encrypt'])) {
            $mobile = Phone::getExtendPhone($orderInfo['consignee_phone_encrypt'], UserExtendPhoneScene::NEW_USER_PACKAGE_CONSIGNEE_PHONE);
        } else {
            $mobile = '';
        }
        $order = [
            'pay_time' => $orderInfo['pay_time'],
            'consignee_name' => $orderInfo['consignee_name'],
            'consignee_phone_encrypt' => $mobile,
            'consignee_info_address' => $orderInfo['consignee_info_address'],
        ];
        // 物流信息
        $logisticsInfo = OrderLogisticsLog::selectInfo(['order_no' => $orderNo]);
        $logistics = PackageLogistics::selectByWhere(['order_no' => $orderNo], ['express_company', 'order_logistics_no', 'status', 'goods_id']);

        $logisticsGoods = array_column($logistics, null, 'goods_id');
        if (isset($logisticsGoods[PackageOrder::PACKAGE_GOOD_1])) {
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['logistic'] = $logisticsInfo[$logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['order_logistics_no']];
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['status'] = PackageLogistics::STATUS_MAP[$logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['status']];
        } else {
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['status'] = PackageLogistics::STATUS_MAP[PackageLogistics::STATUS_NORMAL];
        }
        if (isset($logisticsGoods[PackageOrder::PACKAGE_GOOD_2])) {
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['logistic'] = $logisticsInfo[$logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['order_logistics_no']];
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['status'] = PackageLogistics::STATUS_MAP[$logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['status']];
        } else {
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['status'] = PackageLogistics::STATUS_MAP[PackageLogistics::STATUS_NORMAL];
        }
        if (isset($logisticsGoods[PackageOrder::PACKAGE_GOOD_3])) {
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['logistic'] = $logisticsInfo[$logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['order_logistics_no']];
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['status'] = PackageLogistics::STATUS_MAP[$logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['status']];
        } else {
            $logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['status'] = PackageLogistics::STATUS_MAP[PackageLogistics::STATUS_NORMAL];
        }
        if ($orderInfo['package_id'] == PackageOrder::PACKAGE_ID_1) {
            $goods = [
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_1],
            ];
        } else {
            $goods = [
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_1],
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_2],
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_3],
            ];
        }

        $result['goods'] = $goods;
        $result['order'] = $order;
        $this->setSuccess($result, self::API_CODE_SUCCES);
    }
}